package com.zhoushihao.class0819;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StudentScoreSort {

    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();
        try (BufferedReader br = new BufferedReader(new FileReader("./src/com/neusoft/zhoushihao/class0819/student_score.txt"))) {
            String head = br.readLine();
            String line = br.readLine();
            while (line != null) {
                String[] split = line.split("\\s+");
                Student student = new Student(Integer.parseInt(split[0]), split[1], Integer.parseInt(split[2]), Integer.parseInt(split[3]), Integer.parseInt(split[4]));
                students.add(student);
                line = br.readLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (Student student : students) {
            student.getTotal_score();
            student.getAverageScore();
        }
        Collections.sort(students);
        try (BufferedWriter bw = new BufferedWriter(new FileWriter("./src/com/neusoft/zhoushihao/class0819/student_scoreNew.txt"))) {
            bw.write("学号\t\t姓名\t\t\t语文\t\t数学\t\t英语\t\t平均值\t\t总分\t\t排序");
            bw.newLine();
            for (int i = 0; i < students.size(); i++) {
                Student student = students.get(i);
                String namePadding = student.name.length() <= 2 ? "\t\t\t" : "\t\t";
                String dataLine = student.id + "\t\t" +
                        student.name + namePadding +
                        student.chinese_score + "\t\t" +
                        student.math_score + "\t\t" +
                        student.english_score + "\t\t" +
                        student.average_score + "\t\t" +
                        student.total_score + "\t\t" +
                        (i + 1);
                bw.write(dataLine);
                bw.newLine();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class Student implements Comparable<Student> {
    String name;
    int id;
    int chinese_score;
    int math_score;
    int english_score;
    String average_score;
    int total_score;

    public Student(int id, String name, int chinese_score, int math_score, int english_score) {
        this.name = name;
        this.id = id;
        this.chinese_score = chinese_score;
        this.math_score = math_score;
        this.english_score = english_score;
    }

    public void getTotal_score() {
        int total_score = 0;
        total_score = chinese_score + math_score + english_score;
        this.total_score = total_score;
    }

    public void getAverageScore() {
        double average_score = 0;
        average_score = this.total_score * 1.0 / 3;
        this.average_score = String.format("%.2f", average_score);
    }

    @Override
    public int compareTo(Student o) {
        return o.total_score - this.total_score;
    }
}
